<?php

/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/6
 * Time: 17:38
 */

namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use App\Models\Api\User;
use App\Models\Api\Recode;
use App\Models\Api\MessageCenter;
use App\Models\Like;
use App\Models\Api\Comment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Cache;

class Community extends Model
{
   public $timestamps = false;
   protected $table = 'community';

// 在模型中定义一个方法来获取内容数据
//获取内容
    public function getContentData($map =[]){
        $group_id=1;
        //$type =$map['type'];
        $isGroup=array_key_exists('group_id',$map);
        if($isGroup){
            $group_id=$map['group_id'];
        }
        $good = "SELECT COUNT(community_id) FROM app_community_good WHERE community_id = app_c.id";
        $collection = "SELECT COUNT(community_id) FROM app_collection WHERE community_id = app_c.id";
        if(!empty($map['uid'])){
        // 是否关注
        $isFollow = "SELECT COUNT(follow_uid) FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid";
        // 是否点赞
        $isGood = "SELECT COUNT(community_id) FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.id";
        // 是否收藏
        $isCollection = "SELECT COUNT(community_id) FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.id";
        }
        //如果没有登录账号
        if(empty($map['uid'])){
            if(!$isGroup){
                $list=Db::table('community AS c')
                    ->join('user as u','c.uid', '=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id','=','g.id')
                    ->leftJoin('user_group as ug','u.group ','=', 'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.type',
                        'c.create_time',
                        'u.username',
                        'u.nickname',
                        'ra.sun', 'ra.moon', 'ra.asc',
                        'u.id as uid',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        'c.count_comment as comment',
                        DB::raw("($good) as good"),
                        DB::raw("($collection) as collection"),
                        'watch AS showNumber')
                    ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                    ->orderBy('create_time','desc')
                    ->where('isRemove',0)
                    ->where('r.isself', 1) //自己的档案信息
                    ->get()->toArray();
            }else{
                $list=Db::table('community AS c')
                    ->join('user as u','c.uid', '=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                    ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.type',
                        'c.create_time',
                        'u.username',
                        'u.nickname',
                        'ra.sun', 'ra.moon', 'ra.asc',
                        'u.id as uid',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        DB::raw("($good) as good"),
                        DB::raw("($collection) as collection"),
                       'c.count_comment as comment',
                        'watch AS showNumber')
                    ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                    ->where('c.group_id',$group_id)
                    ->where('isRemove',0)
                    ->where('r.isself', 1) //自己的档案信息
                    ->orderBy('create_time','desc')
                    ->get()->toArray();
            }
        }else{
            if(!$isGroup){
                $list=$this->from('community AS c')
                    ->join('user as u','c.uid', '=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id','=', 'g.id')
                    ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.type',
                        'c.create_time',
                        'u.username',
                        'u.id as uid',
                        'u.nickname',
                        'ra.sun', 
                        'ra.moon', 
                        'ra.asc',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        DB::raw("($good) AS good"),
                        DB::raw("($collection) AS collection"),
                        DB::raw("($isFollow) AS isFollow"),
                        'c.count_comment as comment',
                        DB::raw("($isGood) AS isGood"),
                        DB::raw("($isCollection) AS isCollection"),
                       'watch AS showNumber')
                    ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                    ->where('isRemove',0)
                    ->where('r.isself', 1) //自己的档案信息
                    ->orderBy('create_time','desc')
                    ->get()->toArray();
            }else{
                $list=$this->from('community AS c')
                    ->join('user as u','c.uid','=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                    ->leftJoin('user_group as ug','u.group', '=' ,'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.type',
                        'c.create_time',
                        'u.username',
                        'u.id as uid',
                        'u.nickname',
                        'ra.sun', 'ra.moon', 'ra.asc',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        DB::raw("($good) AS good"),
                        DB::raw("($collection) AS collection"),
                        DB::raw("($isFollow) AS isFollow"),
                        'c.count_comment as comment',
                        DB::raw("($isGood) AS isGood"),
                        DB::raw("($isCollection) AS isCollection"),
                        'watch AS showNumber')
                    ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                    ->where('c.group_id',$group_id)
                    ->where('isRemove',0)
                    ->where('r.isself', 1) //自己的档案信息
                    ->orderBy('create_time','desc')
                    ->get()->toArray();
            }

        }
        
        $per_page = 20;// 当前每页显示数量
        // 统计有多少
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);

        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);

        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
}



    //点赞
    public function sendGood($map=[]){
        $community=$this->isHaveContent($map['id']);
        //看看有没有点赞 有的话就取消 没有的话就点
        $isHaveGood=DB::table("community_good")->where('uid',$map['uid'])->where("community_id",$map['id'])->first();
        if($isHaveGood){
            $res=DB::table("community_good")->where('uid',$map['uid'])->where("community_id",$map['id'])->delete();
            if($res){
                return '-1';
            }else{
                throw new ApiException($res,"取消点赞失败,请稍后重试",200,'10007');
            }
        }else{
            $res=DB::table("community_good")->insert(['uid'=>$map['uid'],'community_id'=>$map['id'],'to_uid'=>$community['uid']]);
            if($res){
                $this->isUserGood($map['id']); //消息通知
                return '1';
            }else{
                throw new ApiException($res,'点赞失败,请稍后重试',200,'10008');
            }
        }
    }
    //收藏说说
    public function sendCollection($map=[]){
        $this->isHaveContent($map['id']);
        //看看有没有收藏 有的话就取消 没有的话就收藏
        $isHaveGood=DB::table("collection")->where('uid',$map['uid'])->where("community_id",$map['id'])->first();
        if($isHaveGood){
            $res=DB::table("collection")->where('uid',$map['uid'])->where("community_id",$map['id'])->delete();
            if($res){
                return '-1';
            }else{
                throw new ApiException($res,"取消收藏失败,请稍后重试",200,'10007');
            }
        }else{
            $res=DB::table("collection")->insert(['uid'=>$map['uid'],'community_id'=>$map['id']]);
            if($res){
                return '1';
            }else{
                throw new ApiException($res,"收藏失败,请稍后重试",200,'10008');
            }
        }
    }
    //举报
    public function sendReport($map=[]){
        $community=$this->isHaveContent($map['id']);
        $report=$this->isReport($map['id'],$map['uid']);
        if($report){
            return false;
        }else{
            $res=DB::table('community_report')->insert(['content'=>$community['content'],'images'=>$community['images'],
                'video'=>$community['video'],'community_id'=>$community['id'],'uid'=>$community['uid'],'report_uid'=>$map['uid']
                ]);
            if($res) return true;
            throw new ApiException($res,"数据添加失败，稍后再试",200,'10008');
        }
    }
    //是否存在该条举报信息
    public function isReport($Id,$userId){
        $res=DB::table('community_report')->where('community_id',$Id)->where('report_uid',$userId)->first();
        if(!$res) return false;
        return $res;
    }
    // //评论一级点赞
    // public function  sendCommentGood($map =[]){
    //     $community=$this->isHaveComment($map['id']);
    //     $isHaveGood=DB::table("comment_good")->where('uid',$map['uid'])->where("comment_id",$map['id'])->first();
    //     if($isHaveGood){
    //         $res=DB::table("comment_good")->where('uid',$map['uid'])->where("comment_id",$map['id'])->delete();
    //         if($res){
    //             return false;
    //         }else{
    //             throw new ApiException($res,"取消点赞失败,请稍后重试",200,'10007');
    //         }
    //     }else{
    //         $res=DB::table("comment_good")->insert(['uid'=>$map['uid'],'comment_id'=>$map['id'],'to_uid'=>$community->uid]);
    //         if($res){
    //             return true;
    //         }else{
    //             throw new ApiException($res,"点赞失败,请稍后重试",200,'10008');
    //         }
    //     }
    // }
    // //二级评论点赞
    // public function sendCommentTwoGood($map=[]){
    //     $community=$this->isHaveCommentTwo($map['id']);
    //     $isHaveGood=DB::table("comment_two_good")->where('uid',$map['uid'])->where("comment_two_id",$map['id'])->first();
    //     if($isHaveGood){
    //         $res=DB::table("comment_two_good")->where('uid',$map['uid'])->where("comment_two_id",$map['id'])->delete();
    //         if($res){
    //             return false;
    //         }else{
    //             throw new ApiException($res,"取消点赞失败,请稍后重试",200,'10007');
    //         }
    //     }else{
    //         $res=DB::table("comment_two_good")->insert(['uid'=>$map['uid'],'comment_two_id'=>$map['id'],'to_uid'=>$community->id]);
    //         if($res){
    //             return true;
    //         }else{
    //             throw new ApiException($res,"点赞失败,请稍后重试",200,'10008');
    //         }
    //     }
    // }
    //发送一级评论
    // public function sendComment($map=[]){
    //     $community=$this->isHaveContent($map['id']);
    //     $res=DB::table('comment')->insertGetId(['uid'=>$map['uid'],'content'=>$map['content'],'community_id'=>$map['id'],'to_uid'=>$community['uid']]);
    //     if($res) return $res;
    //     throw new ApiException($res,"数据添加失败，稍后再试",200,'10008');
    // }
    // //二级评论
    // public function sendCommentTwo($map=[]){
    //     $community=$this->isHaveComment($map['id']);
    //     $res=DB::table('comment_two')->insertGetId(['uid'=>$map['uid'],'content'=>$map['content'],'comment_id'=>$map['id'],'to_uid'=>$community['uid']]);
    //     if($res) return $res;
    //     throw new ApiException($res,"数据添加失败，稍后再试",200,'10008');
    // }
    //有没有该条一级评论的信息
    public function isHaveComment($Id){
        $res=DB::table('comment')->where("id",$Id)->first();
        if(!$res) throw new ApiException("没有找到这条说说",200,'10009');
        return $res;
    }
    //有没有该条二级评论的信息
    public function isHaveCommentTwo($Id){
        $res=DB::table('comment_two')->where("id",$Id)->first();
        if(!$res) throw new ApiException($res,"没有找到这条说说",200,'10009');
        return $res;
    }
    //有没有该条说说信息
    public function isHaveContent($Id){
        $res= $this->where("id",$Id)->first();
        if(!$res) throw new ApiException("没有找到这条说说",200,'10009');
        return $res;
    }
  //如有点赞写入消息通知
    public function isUserGood($Id){
        $res= $this->where("id",$Id)->first();
        if(!empty($res['uid'])){
            // 消息中心添加信息
            $messageData = [
                'uid' => $res['uid'],
                'type' => 3,
                'cid' => 0,
                'title' => '你的圈子动态有点赞',
                'content' => $res['content'],
                'link' =>'getCommunityInfo?id='.$res['id'],
                'target' => "local",
            ];

            //添加消息
            (new MessageCenter())->pushMessage($messageData); 
        }
    }
//     //查看说说下面的评论
// public function getComment($map = [])
// {
//     $this->isHaveContent($map['id']);
//     $good = "SELECT COUNT(comment_id) FROM app_comment_good WHERE comment_id = app_c.id";
//     if(!$map){
//             $res=Db::table('comment AS c')
//                 ->join('user as u','c.uid', '=','u.id')
//                 ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
//                 ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
//                 ->where('community_id',$map['id'])
//                 ->where('r.isself',1)
//                 ->select(
//                     'c.id',
//                     'c.content',
//                     'c.create_time',
//                     'u.username',
//                     'u.id as uid',
//                     'u.nickname',
//                     'ra.sun', 
//                     'ra.moon', 
//                     'ra.asc',
//                     'u.avatar',
//                     DB::raw("$good) as good"))
//                 ->orderBy('good','desc')
//                 ->get()->toArray();
//             foreach ($res as $key=>$value){
//                 $cId=$value->id;
//                 $twoGood = "SELECT COUNT(comment_two_id) FROM app_comment_two_good WHERE comment_two_id = app_t.id";
//                 $twoRes=Db::table('comment_two AS t')
//                     ->join('user as u','t.uid','=','u.id')
//                     ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
//                     ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
//                     ->where('comment_id',$cId)
//                     ->where('r.isself',1)
//                     ->select(
//                         't.id',
//                         't.content',
//                         't.create_time',
//                         'u.username',
//                         'u.id as uid',
//                         'u.nickname',
//                         'ra.sun', 
//                         'ra.moon', 
//                         'ra.asc',
//                         'u.avatar',
//                         DB::raw("($twoGood) as good"))
//                     ->orderBy('good','desc')
//                     ->get()->toArray();
//                 $res[$key]->twoComment=$twoRes;
//             }
//         }else{
//             $isGood = "SELECT COUNT(comment_id) FROM app_comment_good WHERE comment_id = app_c.id AND uid = {$map['uid']}";
//             $res=Db::table('comment AS c')
//                 ->join('user as u','c.uid', '=','u.id')
//                 ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
//                 ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
//                 ->where('community_id',$map['id'])
//                 ->where('r.isself',1)
//                 ->select(
//                     'c.id',
//                     'c.content',
//                     'c.create_time',
//                     'u.username',
//                     'u.id as uid',
//                     'u.nickname',
//                     'ra.sun', 
//                     'ra.moon', 
//                     'ra.asc',
//                     'u.avatar',
//                     DB::raw("($good) as good"),
//                     DB::raw("($isGood) as isGood"))
//                 ->orderBy('good','desc')
//                 ->get()->toArray();
//             foreach ($res as $key=>$value){
//                 $cId=$value->id;
//                 $twoGood = "SELECT COUNT(comment_two_id) FROM app_comment_two_good WHERE comment_two_id = app_t.id";
//                 $twoIsGood = "SELECT COUNT(comment_two_id) FROM app_comment_two_good WHERE comment_two_id = app_t.id AND uid = {$map['uid']}";
//                 $twoRes=Db::table('comment_two AS t')
//                     ->join('user as u','t.uid','=', 'u.id')
//                     ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
//                     ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
//                     ->where('comment_id',$cId)
//                     ->where('r.isself',1)
//                     ->select(
//                         't.id',
//                         't.content',
//                         't.create_time',
//                         'u.username',
//                         'u.id as uid',
//                         'u.nickname',
//                         'ra.sun', 
//                         'ra.moon', 
//                         'ra.asc',
//                         'u.avatar',
//                         DB::raw("($twoGood) as good"),
//                         DB::raw("($twoIsGood) as isGood"))
//                     ->orderBy('good','desc')
//                   // ->limit(0,20)
//                     ->get()->toArray();
//                 $res[$key]->twoComment=$twoRes;
//             }
//         }

//         $per_page = 20;// 当前订单每页显示数量
//         // 统计有多少订单
//         $total = count($res);
//         // 获取数据分页信息
//         $data_list = getPaginatorData($total, $map['page'], $per_page);

//         // 获取分页后的订单id数组
//         $alllist = array_slice($res, $data_list['start'], $per_page);

//         unset($data_list['start']);
//         unset($data_list['end']);
//         unset($data_list['data']);
//         $data_list['list']=$alllist;
//       return $data_list;
// }
//     //获取二级评论
//     public function getCommentTwo($map =[]){
//         $this->isHaveContent($map['id']);
//         //点赞查询
//         if(!$map['uid']){
//             $twoRes=DB::table('comment_two AS t')
//                 ->join('user as u','t.uid', '=', 'u.id')
//                 ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
//                 ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
//                 ->where('comment_id',$map['id'])
//                 ->where('r.isself',1)
//                 ->select(
//                     't.id',
//                     't.content',
//                     't.create_time',
//                     'u.username',
//                     'u.id as uid',
//                     'u.nickname',
//                     'ra.sun', 
//                     'ra.moon',
//                     'ra.asc',
//                     'u.avatar',
//                   DB::raw('SELECT count(comment_two_id) FROM app_comment_two_good WHERE comment_two_id = app_t.id) as good'))
//                 ->orderBy('good','desc')->get()->toArray();
//         }else{
//             $twoRes=DB::table('comment_two AS t')
//                 ->join('user as u','t.uid', '=', 'u.id')
//                 ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
//                 ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
//                 ->where('comment_id',$map['id'])
//                 ->where('r.isself',1)
//                 ->select(
//                     't.id',
//                     't.content',
//                     't.create_time',
//                     'u.username',
//                     'u.id as uid',
//                     'u.nickname',
//                     'ra.sun', 
//                     'ra.moon',
//                     'ra.asc',
//                     'u.avatar',
//                   DB::raw("(SELECT count(comment_two_id) FROM app_comment_two_good WHERE comment_two_id = app_t.id) as good"),
//                   DB::raw("(SELECT count(comment_two_id) FROM app_comment_two_good WHERE comment_two_id = app_t.id AND uid = {$map['uid']}) AS isGood"))
//                 ->orderBy('good','desc')
//                 ->get()->toArray();
//         }
//         $per_page = 20;// 当前订单每页显示数量
//       // var_dump($list);die;
//         // 统计有多少订单
//         $total = count($twoRes);
//         // 获取数据分页信息
//         $data_list = getPaginatorData($total, $map['page'], $per_page);

//         // 获取分页后的订单id数组
//         $alllist = array_slice($twoRes, $data_list['start'], $per_page);

//         unset($data_list['start']);
//         unset($data_list['end']);
//         unset($data_list['data']);
//         $data_list['list']=$alllist;
//       return $data_list;
//     }
    public function sendFollow($map =[]){
        $this->isHaveUser($map['id']);
        $isHaveFollow=$this->isHaveFollow($map['uid'],$map['id']);
        if($isHaveFollow){
            DB::table('community_follow')->where("uid",$map['uid'])->where('follow_uid',$map['id'])->delete();
            return false;
        }else{
            $res=DB::table('community_follow')->insert(['uid'=>$map['uid'],'follow_uid'=>$map['id']]);
            return true;
        }
        throw new ApiException("关注异常，稍后再试",200,'10000');
    }
    //是否存在该用户
    public function isHaveUser($Id){
        $res=DB::table('user')->where("id",$Id)->first(['id','uen','token','token_expire','tel','uc_id','nickname','sex','avatar','status','has_self_recode','forbidden','regid']);
        if(!$res) throw new ApiException("没有找到该用户",200,'10009');
        return $res;
    }
    //是否关注该用户
    public function isHaveFollow($my,$he){
        $res=DB::table('community_follow')->where("uid",$my)->where('follow_uid',$he)->first();
        if(!$res) return false;
        return true;
    }
    //获取社区的类别
    public function getCommunityType(){
        $res=DB::table('community_group')->get();
        return $res;
    }
    //发送动态内容
    public function sendContent($map =[]){
        if($map['isTop']){
            $isTop=1;
        }else{
            $isTop=0;
        }
        $res=DB::table('community')->insert(
            [
                'uid'=>$map['uid'],
                'content'=>$map['content'],
                'images'=>$map['images'],
                'video'=>$map['video'],
                'group_id'=>$map['group_id'],
                'address'=>$map['address'],
                'location'=>$map['location'],
                'type'=>$map['type'],
                'isTop'=>$isTop
            ]);
        if($res) return true;
        throw new ApiException("发布异常，稍后再试",200,'10000');
    }

//获取动态信息
public function getCommunityInfo($map = [])
{ 
    //如果没有登录账号
    if (!$map['uid']) {
        $list = DB::table('community AS c')
            ->join('user AS u', 'c.uid', '=', 'u.id')
            ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
            ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
            ->leftJoin('community_group AS g', 'c.group_id', '=', 'g.id')
            ->leftJoin('user_group AS ug', 'u.group', '=', 'ug.id')
            ->select([
                'c.id',
                'c.content',
                'c.address',
                'c.location',
                'c.images',
                'c.video',
                'c.type',
                'c.create_time',
                'u.username',
                'u.nickname',
                'u.id AS uid',
                'u.avatar',
                'ra.sun', 'ra.moon', 'ra.asc',
                'g.name AS groupName',
                'g.id AS groupId',
                'ug.id AS userGroupId',
                'ug.name AS userGroupName',
                'ug.type AS userGroupType',
                DB::raw('(SELECT COUNT(community_id) FROM app_community_good WHERE community_id = c.id) AS good'),
                DB::raw('(SELECT COUNT(community_id) FROM app_collection WHERE community_id = c.id) AS collection'),
                'c.count_comment as comment',
                DB::raw('(SELECT COUNT(community_id) FROM app_community_show WHERE community_id = c.id) AS showNumber')
            ])
            ->where('c.id', $map['id'])
            ->where('isRemove', 0)
            ->where('r.isself',1)
            ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
            //->limit(1)
            ->first();
    } else {
        
        $this->where('id', $map['id'])->increment('watch', '1');
        $list = DB::table('community AS c')
            ->join('user AS u', 'c.uid', '=', 'u.id')
            ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
            ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
            ->leftJoin('community_group AS g', 'c.group_id', '=', 'g.id')
            ->leftJoin('user_group AS ug', 'u.group', '=', 'ug.id')
            ->select([
                'c.id',
                'c.content',
                'c.address',
                'c.location',
                'c.images',
                'c.video',
                'c.type',
                'c.create_time',
                'u.username',
                'u.nickname',
                'u.id AS uid',
                'u.avatar',
                'ra.sun', 'ra.moon', 'ra.asc',
                'g.name AS groupName',
                'g.id AS groupId',
                'ug.id AS userGroupId',
                'ug.name AS userGroupName',
                'ug.type AS userGroupType',
                DB::raw('(SELECT COUNT(community_id) FROM app_community_good WHERE community_id = app_c.id) AS good'),
                DB::raw('(SELECT COUNT(community_id) FROM app_collection WHERE community_id = app_c.id) AS collection'),
               'c.count_comment as comment',
               'watch AS showNumber',
                DB::raw("(SELECT COUNT(follow_uid) FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid) AS isFollow"),
                DB::raw("(SELECT COUNT(community_id) FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.id) AS isGood"),
                DB::raw("(SELECT COUNT(community_id) FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.id) AS isCollection")
            ])
            ->where('c.id', $map['id'])
            ->where('isRemove', 0)
            ->where('r.isself',1)
            ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
           // ->limit(1)
            ->first();
    }
    
    return $list;
}



  public function getMyFollow($map = [])
{
   $goodSql = "SELECT COUNT(cg.community_id) AS good_count
                FROM app_community_good cg
                WHERE cg.community_id = app_c.id";
                
    $collectionSql = "SELECT COUNT(col.community_id) AS collection_count
                     FROM app_collection col
                     WHERE col.community_id = app_c.id";

    $commentSql = "SELECT COUNT(com.community_id) AS comment_count
                   FROM app_comment com
                   WHERE com.community_id = app_c.id";

    $isFollowSql = "SELECT COUNT(cf.follow_uid) AS is_follow
                    FROM app_community_follow cf
                    WHERE cf.uid = {$map['uid']}
                    AND cf.follow_uid = app_c.uid";

    $isGoodSql = "SELECT COUNT(cg.community_id) AS is_good
                  FROM app_community_good cg
                  WHERE cg.uid = {$map['uid']}
                  AND cg.community_id = app_c.id";

    $isCollectionSql = "SELECT COUNT(col.community_id) AS is_collection
                        FROM app_collection col
                        WHERE col.uid = {$map['uid']}
                        AND col.community_id = app_c.id";

    $list = DB::table('community AS c')
        ->join('user as u', 'c.uid', '=', 'u.id')
        ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
        ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
        ->leftJoin('community_group as g', 'c.group_id', '=', 'g.id')
        ->leftJoin('community_follow as f', 'f.follow_uid', '=', 'c.uid')
        ->leftJoin('user_group as ug', 'u.group', '=', 'ug.id')
        ->select(
            'c.id',
            'c.content',
            'c.address',
            'c.location',
            'c.images',
            'c.video',
            'c.type',
            'c.create_time',
            'u.username',
            'u.id as uid',
            'u.nickname',
            'ra.sun', 'ra.moon', 'ra.asc',
            'u.avatar',
            'g.name as groupName',
            'g.id as groupId',
            'ug.id as userGroupId',
            'ug.name as userGroupName',
            'ug.type as userGroupType',
            DB::raw("($goodSql) as good"),
            DB::raw("($collectionSql) as collection"),
            DB::raw("($isFollowSql) as isFollow"),
            'c.count_comment as comment',
            DB::raw("($isGoodSql) as isGood"),
            DB::raw("($isCollectionSql) as isCollection"),
            'watch AS showNumber'
        )
        ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
        ->where('f.uid', $map['uid'])
        ->where('isRemove', 0)
        ->where('r.isself',1)
        ->orderBy('create_time', 'desc')
        ->get()->toArray();

       $per_page = 20;// 当前订单每页显示数量
       // var_dump($list);die;
        // 统计有多少订单
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);

        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);

        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
}
    //获取点赞评论最多的说说
    public function getCommunityFire($map =[]){
        $good = "SELECT COUNT(community_id) as good_count FROM app_community_good WHERE community_id = app_c.id";
        $collection = "SELECT COUNT(community_id) as collection_count FROM app_collection WHERE community_id = app_c.id";
        //如果没有登录账号
        if(empty($map['uid'])){
            $list=DB::table('community AS c')
                ->join('user as u','c.uid', '=', 'u.id')
                ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                ->select(
                   'c.id',
                   'c.content',
                   'c.address',
                   'c.location',
                   'c.images',
                   'c.video',
                   'c.type',
                   'c.create_time',
                   'u.username',
                   'u.nickname',
                   'u.id as uid',
                   'ra.sun', 'ra.moon', 'ra.asc',
                   'u.avatar',
                   'g.name as groupName',
                   'g.id as groupId',
                   'ug.id as userGroupId',
                   'ug.name as userGroupName',
                   'ug.type as userGroupType',
                   DB::raw("($good) as good"),
                   DB::raw("($collection) as collection"),
                   'c.count_comment as comment',
                   'watch AS showNumber')
                ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                ->where('r.isself',1)
                ->where('isRemove',0)
                ->orderBy('showNumber','desc')
                ->get()->toArray();
        }else{
            //是否关注
           $isFollow = "SELECT COUNT(follow_uid) FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid";

           $isGood = "SELECT COUNT(community_id) FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.id";

           $isCollection = "SELECT COUNT(community_id) FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.id";

            $list=DB::table('community as c')
                ->join('user as u','c.uid','=','u.id')
                ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                ->select(
                  'c.id',
                  'c.content',
                  'c.address',
                  'c.location',
                  'c.images',
                  'c.video',
                  'c.type',
                  'c.create_time',
                  'u.username',
                  'u.id as uid',
                  'u.nickname',
                  'ra.sun', 'ra.moon', 'ra.asc',
                  'u.avatar',
                  'g.name as groupName',
                  'g.id as groupId',
                  'ug.id as userGroupId',
                  'ug.name as userGroupName',
                  'ug.type as userGroupType',
                  DB::raw("($good) as good"),
                  DB::raw("($collection) as collection"),
                  DB::raw("($isFollow) as isFollow"),
                  'c.count_comment as comment',
                  DB::raw("($isGood) as isGood"),
                  DB::raw("($isCollection) as isCollection"),
                  'watch AS showNumber')
                ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                ->where('r.isself',1)
                ->where('isRemove',0)
                ->orderBy('showNumber','desc')
                ->get()->toArray();
        }
        
        $per_page = 20;// 当前每页显示数量
       // var_dump($list);die;
        // 统计有多少订单
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);
       
        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);
        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
    }
    //删除动态
    public function removeCommunity($map =[]){
        $res=DB::table('community')->where('uid',$map['uid'])->where('id',$map['id'])->update(
            [
                'isRemove'=>1
            ]);
        if($res) return true;
        throw new ApiException("删除异常，稍后再试",200,'10000');
    }
    //获取用户动态
    public function getUserCommunity($map =[]){
    $good = "SELECT COUNT(cg.community_id) AS good_count FROM app_community_good cg WHERE cg.community_id = app_c.id";
    $collection = "SELECT COUNT(col.community_id) AS collection_count FROM app_collection col WHERE col.community_id = app_c.id";
        //如果没有登录账号
        if(!$map['uid']){

            $list=DB::table('community AS c')
                ->join('user as u', 'c.uid', '=', 'u.id')
                ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                ->leftJoin('community_group as g','c.group_id','=', 'g.id')
                ->leftJoin('user_group as ug','u.group','=', 'ug.id')
                ->select(
                    'c.id',
                    'c.content',
                    'c.address',
                    'c.location',
                    'c.images',
                    'c.video',
                    'c.type',
                    'c.create_time',
                    'u.username',
                    'u.nickname',
                    'u.id as uid',
                    'ra.sun', 'ra.moon', 'ra.asc',
                    'u.avatar',
                    'g.name as groupName',
                    'g.id as groupId',
                    'ug.id as userGroupId',
                    'ug.name as userGroupName',
                    'ug.type as userGroupType',
                    DB::raw("($good) as good"),
                    DB::raw("($collection) as collection"),
                    'c.count_comment as comment',
                    'watch AS showNumber')
                ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                ->orderBy('create_time','desc')
                ->where('isRemove',0)
                ->where('r.isself',1)
                ->where('c.uid',$map['id'])
                ->get()->toArray();

        }else{
            // 是否关注
            $isFollow = "SELECT COUNT(cf.follow_uid) FROM app_community_follow cf  WHERE cf.uid = {$map['uid']} AND cf.follow_uid = app_c.uid";
            
            // 是否点赞
            $isGood = "SELECT COUNT(cg.community_id) FROM app_community_good  cg WHERE cg.uid = {$map['uid']} AND cg.community_id = app_c.id";
            
            // 是否收藏
            $isCollection = "SELECT COUNT(col.community_id) FROM app_collection col WHERE col.uid = {$map['uid']} AND col.community_id = app_c.id";
            $list=DB::table('community AS c')
                ->join('user as u', 'c.uid', '=', 'u.id')
                ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                ->leftJoin('community_group as g','c.group_id','=', 'g.id')
                ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                ->select(
                    'c.id',
                    'c.content',
                    'c.address',
                    'c.location',
                    'c.images',
                    'c.video',
                    'c.type',
                    'c.create_time',
                    'u.username',
                    'u.nickname',
                    'u.id as uid',
                    'u.avatar',
                    'ra.sun', 'ra.moon', 'ra.asc',
                    'g.name as groupName',
                    'g.id as groupId',
                    'ug.id as userGroupId',
                    'ug.name as userGroupName',
                    'ug.type as userGroupType',
                    DB::raw("($good) as good"),
                    DB::raw("($collection) as collection"),
                    DB::raw("($isFollow) as isFollow"),
                   'c.count_comment as comment',
                    DB::raw("($isGood) as isGood"),
                    DB::raw("($isCollection) as isCollection"),
                    'watch AS showNumber')
                ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                ->where('isRemove',0)
                ->where('r.isself',1)
                ->where('c.uid',$map['id'])
                ->orderBy('create_time','desc')
                ->get()->toArray();

        }
        
        $per_page = 20;// 当前每页显示数量
       // var_dump($list);die;
        // 统计有多少订单
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);
       
        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);
        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
    }
    public function getUserCommunityIndex($map =[]){
        //获取动态获赞总数
        $communityGood=DB::table('community_good')
                      ->select(DB::raw('count(*) as good'))
                      ->where('to_uid', $map['id'])
                      ->first();
        //评论点赞数
        $like =  Comment::where('to_uid', $map['id'])
          ->withCount('likes')
         ->sum('count_like');
        $good=$communityGood->good+$like;
        $myFollow=DB::table('community_follow')->select(DB::raw('count(*) as myFollow'))->where('uid',$map['id'])->first();
        $followMe=DB::table('community_follow')->select(DB::raw('count(*) as followMe'))->where('follow_uid',$map['id'])->first();
        //var_dump($map);die;
        //$userInfo=(new User())->getUserinfo($map);
        $userInfo = $this->isHaveUser($map['id']);
        $res=[
            'userInfo'=>$userInfo,
            'good'=>$good,
            'myFollow'=>$myFollow->myFollow,
            'followMe'=>$followMe->followMe
        ];
        return $res;

    }
    //获取我的收藏
    public function myCollection($map=[]){
        //圈子点赞
        $good = "SELECT COUNT(community_id) as good FROM app_community_good WHERE community_id = app_c.id";
        //收藏
        $collection = "SELECT COUNT(community_id) as collection FROM app_collection WHERE community_id = app_c.id";
         //是否关注
        $isFollow = "SELECT COUNT(follow_uid) as isFollow FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid";
        //是否点赞
        $isGood = "SELECT COUNT(community_id) as isGood FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.id";
        //是否收藏
        $isCollection = "SELECT COUNT(community_id) as isCollection FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.id";



        $list=DB::table('community AS c')
            ->join('user as u','c.uid', '=','u.id')
            ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
            ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
            ->leftJoin('community_group as g','c.group_id', '=','g.id')
            ->rightJoin('collection as coll','c.id', '=', 'coll.community_id')
            ->leftJoin('user_group as ug','u.group','=', 'ug.id')
            ->select(
                'c.id',
                'c.content',
                'c.address',
                'c.location',
                'c.images',
                'c.video',
                'c.type',
                'c.create_time',
                'u.username',
                'u.id as uid',
                'u.nickname',
                'ra.sun', 'ra.moon', 'ra.asc',
                'u.avatar',
                'g.name as groupName',
                'g.id as groupId',
                'ug.id as userGroupId',
                'ug.name as userGroupName',
                'ug.type as userGroupType',
                DB::raw("($good) as good"),
                DB::raw("($collection) as collection"),
                DB::raw("($isFollow) as isFollow"),
                'c.count_comment as comment',
                DB::raw("($isGood) as isGood"),
                DB::raw("($isCollection) as isCollection"),
                'watch AS showNumber')
            ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
            ->where('isRemove',0)
            ->where('r.isself',1)
            ->where('coll.uid',$map['uid'])
            ->orderBy('create_time','desc')
            ->get()->toArray();
     
        $per_page = 20;// 当前每页显示数量
       // var_dump($list);die;
        // 统计有多少订单
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);
       
        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);
        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
    }

     //获取我的点赞
    public function myGood($map=[]){
        //圈子点赞
        $good = "SELECT COUNT(community_id) as good FROM app_community_good WHERE community_id = app_c.id";
        //收藏
        $collection = "SELECT COUNT(community_id) as collection FROM app_collection WHERE community_id = app_c.id";
         //是否关注
        $isFollow = "SELECT COUNT(follow_uid) as isFollow FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid";
        //是否点赞
        $isGood = "SELECT COUNT(community_id) as isGood FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.id";
        //是否收藏
        $isCollection = "SELECT COUNT(community_id) as isCollection FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.id";



        $list=DB::table('community AS c')
            ->join('user as u','c.uid', '=','u.id')
            ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
            ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
            ->leftJoin('community_group as g','c.group_id', '=','g.id')
            ->rightJoin('community_good as good','c.id', '=', 'good.community_id')
            ->leftJoin('user_group as ug','u.group','=', 'ug.id')
            ->select(
                'c.id',
                'c.content',
                'c.address',
                'c.location',
                'c.images',
                'c.video',
                'c.type',
                'c.create_time',
                'u.username',
                'u.id as uid',
                'u.nickname',
                'ra.sun', 'ra.moon', 'ra.asc',
                'u.avatar',
                'g.name as groupName',
                'g.id as groupId',
                'ug.id as userGroupId',
                'ug.name as userGroupName',
                'ug.type as userGroupType',
                DB::raw("($good) as good"),
                DB::raw("($collection) as collection"),
                DB::raw("($isFollow) as isFollow"),
                'c.count_comment as comment',
                DB::raw("($isGood) as isGood"),
                DB::raw("($isCollection) as isCollection"),
                'watch AS showNumber')
            ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
            ->where('isRemove',0)
            ->where('r.isself',1)
            ->where('good.uid',$map['uid'])
            ->orderBy('create_time','desc')
            ->get()->toArray();
     
        $per_page = 20;// 当前每页显示数量
       // var_dump($list);die;
        // 统计有多少订单
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);
       
        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);
        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
    }
    //添加访客
    public function addVisitorNumber($map =[]){
        $res=DB::table('community_show')->insert(['uid'=>1,'community_id'=>$map['id']]);
        if($res)return $res;
        throw new ApiException("请求异常，稍后再试",200,'10000');
    }
    
    //热门功能
    public function getCommunityTop(){
        
        $good = "SELECT COUNT(community_id) as good FROM app_community_good WHERE community_id = app_c.id";
        $collection = "SELECT COUNT(community_id) as collection FROM app_collection WHERE community_id = app_c.id";
        $list=DB::table('community as c')
            ->join('user as u','c.uid', '=','u.id')
            ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
            ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
            ->leftJoin('community_group as g','c.group_id', '=','g.id')
            ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
            ->select(
                'c.id',
                'c.content',
                'c.address',
                'c.location',
                'c.images',
                'c.video',
                'c.type',
                'c.create_time',
                'c.isTop',
                'u.username',
                'u.nickname',
                'u.id as uid',
                'ra.sun', 'ra.moon', 'ra.asc',
                'u.avatar',
                'g.name as groupName',
                'g.id as groupId',
                'ug.id as userGroupId',
                'ug.name as userGroupName',
                'ug.type as userGroupType',
                DB::raw("($good) as good"),
                DB::raw("($collection) as collection"),
               'c.count_comment as comment',
                'watch AS showNumber')
            ->groupBy('c.id','ra.sun','ra.moon','ra.asc')
            ->where('c.isTop',1)
            ->where('r.isself',1)
            ->orderBy('create_time','desc')
            ->where('isRemove',0)
            ->get();
        return $list;
    }
    //取消置顶
    public function closeTop($map =[]){
        $res=DB::table('community')->where('id',$map['id'])->update(['isTop'=>0]);
        if($res)return $res;
        throw new ApiException("请求异常，稍后再试",200,'10000');
    }
    //搜索
    public function searchAll($map =[]){
        
        $good = "SELECT COUNT(community_id) as good FROM app_community_good WHERE community_id = app_c.id";
        $collection = "SELECT COUNT(community_id) as collection FROM app_collection WHERE community_id = app_c.id";
        //如果没有登录账号
        if(!$map['uid']){
                DB::table('search')->insert(['title'=>$map['searchValue']]);
                $list=DB::table('community AS c')
                    ->join('user as u','c.uid', '=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                    ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.type',
                        'c.create_time',
                        'u.username',
                        'u.nickname',
                        'u.id as uid',
                        'ra.sun','ra.moon','ra.asc',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        DB::raw("($good) as good"),
                        DB::raw("($collection) as collection"),
                        'c.count_comment as comment',
                        'watch AS showNumber')
                    ->groupBy('c.id','ra.sun','ra.moon','ra.asc')
                    ->orderBY('create_time','desc')
                    ->where('isRemove',0)
                    ->where('r.isself',1)
                    ->where('content','LIKE','%' .$map['searchValue'].'%')
                    ->get()->toArray();
        }else{
            DB::table('search')->insert(['title'=>$map['searchValue'],'uid'=>$map['uid']]);
            //是否关注
            $isFollow = "SELECT COUNT(follow_uid) as isFollow FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid";
            //是否点赞
            $isGood = "SELECT COUNT(community_id) as isGood FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.id";
            //是否收藏
            $isCollection = "SELECT COUNT(community_id) as isCollection FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.id";

            $list=DB::table('community AS c')
                ->join('user as u','c.uid', '=', 'u.id')
                ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                ->select(
                    'c.id',
                    'c.content',
                    'c.address',
                    'c.location',
                    'c.images',
                    'c.video',
                    'c.type',
                    'c.create_time',
                    'u.username',
                    'u.id as uid',
                    'u.nickname',
                    'ra.sun','ra.moon','ra.asc',
                    'u.avatar',
                    'g.name as groupName',
                    'g.id as groupId',
                    'ug.id as userGroupId',
                    'ug.name as userGroupName',
                    'ug.type as userGroupType',
                    DB::raw("($good) as good"),
                    DB::raw("($collection) as collection"),
                    DB::raw("($isFollow) as isFollow"),
                    'c.count_comment as comment',
                    DB::raw("($isGood) as isGood"),
                    DB::raw("($isCollection) as isCollection"),
                    'watch AS showNumber')
                ->groupBy('c.id','ra.sun','ra.moon','ra.asc')
                ->where('isRemove',0)
                ->where('r.isself',1)
                ->orderBy('create_time','desc')
                ->where('content','LIKE','%' .$map['searchValue'].'%')
                ->get()->toArray();
        }
       
       $per_page = 20;// 当前每页显示数量
       // var_dump($list);die;
        // 统计有多少订单
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);
       
        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);
        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
    }
    
    public function getHotSearch(){
        
        $res = DB::table('search')
                ->select('title', DB::raw('count(*) as num'))
                ->groupBy('title')
                ->orderBy('num', 'desc')
                ->take(5)
                ->get();
        return $res;
    }
 // video 视频播放专用  
 public function getVideoData($map =[]){
        $group_id = 1;
        $type =$map['type'];
        $isGroup=array_key_exists('group_id',$map);
        if($isGroup){
            $group_id=$map['group_id'];
        }
        $good = "SELECT COUNT(community_id) FROM app_community_good WHERE community_id = app_c.id";
        $collection = "SELECT COUNT(community_id) FROM app_collection WHERE community_id = app_c.id";
        // 是否关注
        $isFollow = "SELECT COUNT(follow_uid) FROM app_community_follow WHERE uid = {$map['uid']} AND follow_uid = app_c.uid";
        // 是否点赞
        $isGood = "SELECT COUNT(community_id) FROM app_community_good WHERE uid = {$map['uid']} AND community_id = app_c.Id";
        // 是否收藏
        $isCollection = "SELECT COUNT(community_id) FROM app_collection WHERE uid = {$map['uid']} AND community_id = app_c.Id";
        if(!$isGroup){
                $list=$this->from('community AS c')
                    ->join('user as u','c.uid', '=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id','=', 'g.id')
                    ->leftJoin('user_group as ug','u.group', '=', 'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.type',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.create_time',
                        'u.username',
                        'u.id as uid',
                        'u.nickname',
                        'ra.sun', 
                        'ra.moon', 
                        'ra.asc',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        DB::raw("($good) AS good"),
                        DB::raw("($collection) AS collection"),
                        DB::raw("($isFollow) AS isFollow"),
                        'c.count_comment as comment',
                        DB::raw("($isGood) AS isGood"),
                        DB::raw("($isCollection) AS isCollection"),
                        'watch AS showNumber')
                    ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                    ->where('isRemove',0)
                    ->where('c.type',$type)
                    ->where('r.isself', 1) //自己的档案信息
                    ->orderBy('create_time','desc')
                    ->get()->toArray();
            }else{
                $list=$this->from('community AS c')
                    ->join('user as u','c.uid','=', 'u.id')
                    ->leftJoin('recode as r', 'r.uid', '=', 'u.id')
                    ->leftJoin('recode_astro as ra', 'r.id', '=', 'ra.rid')
                    ->leftJoin('community_group as g','c.group_id', '=', 'g.id')
                    ->leftJoin('user_group as ug','u.group', '=' ,'ug.id')
                    ->select(
                        'c.id',
                        'c.content',
                        'c.address',
                        'c.type',
                        'c.location',
                        'c.images',
                        'c.video',
                        'c.create_time',
                        'u.username',
                        'u.id as uid',
                        'u.nickname',
                        'ra.sun', 'ra.moon', 'ra.asc',
                        'u.avatar',
                        'g.name as groupName',
                        'g.id as groupId',
                        'ug.id as userGroupId',
                        'ug.name as userGroupName',
                        'ug.type as userGroupType',
                        DB::raw("($good) AS good"),
                        DB::raw("($collection) AS collection"),
                        DB::raw("($isFollow) AS isFollow"),
                        'c.count_comment as comment',
                        DB::raw("($isGood) AS isGood"),
                        DB::raw("($isCollection) AS isCollection"),
                        'watch AS showNumber')
                    ->groupBy('c.id','ra.sun', 'ra.moon', 'ra.asc')
                    ->where('c.group_id',$group_id)
                    ->where('c.type',$type)
                    ->where('isRemove',0)
                    ->where('r.isself', 1) //自己的档案信息
                    //->orderBy('create_time','desc')
                    ->orderByRaw("CASE WHEN app_c.id = ? THEN 0 ELSE 1 END, id asc", [$map['id']])
                    ->get()->toArray();

        }


        $per_page = 20;// 当前每页显示数量
        // 统计有多少
        $total = count($list);
        // 获取数据分页信息
        $data_list = getPaginatorData($total, $map['page'], $per_page);

        // 获取分页后的订单id数组
        $alllist = array_slice($list, $data_list['start'], $per_page);

        unset($data_list['start']);
        unset($data_list['end']);
        unset($data_list['data']);
        $data_list['list']=$alllist;
      return $data_list;
}

   public function user(){
      
        return $this->hasOne('App\Models\Api\User','id','uid');
   }
    /**
     * 当前登录用户是否已经点赞
     *
     * @param bool $small
     * @return bool
     */
    public function isLike($small = false,$uid)
    {
        if(!$uid){
            return false;
        }
        if (!$small) {
            return $this->likes()->contains('uid', $uid);
        } else {
            return $this->likes()->where('uid',$uid)->first() ? true : false;
        }
    }
     // 多态关联获取评论的赞
    public function likes()
    {
        return $this->morphMany(Like::class, 'likeable');
    }
    // 一对多获取圈子评论
    public function comments($kind='all',$user_id='')
    {
        $model = $this->hasMany(Comment::class);
        // 只获取顶级评论
        if ($kind=='p') {
            $model->where('p_id',0);
        }
        // 只获取子评论
        if ($kind=='c') {
            $model->where('p_id','!=',0);
        }
        
        return $model;
    }
}